﻿using DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;

namespace MyWeb.GUI.User
{
    public partial class SanPham : System.Web.UI.Page
    {
        DataProvider db = new DataProvider();
        private int MaHang { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["MaHangSX"] != null)
            {
                MaHang = Convert.ToInt16(Request["MaHangSX"]);
            }
            else
            {
                MaHang = 0;
            }
            if (!Page.IsPostBack)
            {
                LoadSanPham();
                LoadHangSX();
                LoadHangBanChay();
            }
        }
        private void LoadSanPham()
        {
            if (MaHang == 0)
            {
                rpSanPham.DataSource = db.LayDuLieuThamSo("LoadDanhSachSanPham", CommandType.StoredProcedure, new object[] { "@MaHang", 0 });
                rpSanPham.DataBind();
            }
            else
            {
                rpSanPham.DataSource = db.LayDuLieuThamSo("LoadDanhSachSanPham", CommandType.StoredProcedure, new object[] { "@MaHang", MaHang });
                rpSanPham.DataBind();
            }
        }
        private void LoadHangSX()
        {
            rploadhangsx.DataSource = db.LayDuLieuThamSo("Danhsachhang", CommandType.StoredProcedure);
            rploadhangsx.DataBind();
        }
        private void LoadHangBanChay()
        {
            rpHangbanchay.DataSource = db.LayDuLieuThamSo("LoadHangBanChay",CommandType.StoredProcedure);
            rpHangbanchay.DataBind();
        }

        protected void rpSanPham_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName.Equals("AddCart"))
            {
                int masp = Convert.ToInt32(e.CommandArgument);
               BUS.ShoppingCart.Instance.AddItem(masp);
                Response.Redirect("TrangChu.aspx");
            }


            if (e.CommandName.Equals("Detail"))
            {
                int proId = Convert.ToInt32(e.CommandArgument);
                Response.Redirect("ChiTietSanPham.aspx?MaSP=" + proId);
            }
        }
    }
}